package com.ttl.bc.report.distribution;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JasperRunManager;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class RptDistributionAction extends Action {

	@SuppressWarnings("unchecked")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		RptDistributionForm generateReportForm = (RptDistributionForm)form;
		String monthId = generateReportForm.getMonthId();
		String yearId = generateReportForm.getYearId();
		String ttlStr = generateReportForm.getTtlStr();
		String monthStr[] = { "January","February", "March", "April", "May", "June", 
							  "July","August", "September", "October", "November", "December"};
//		System.out.println(generateReportForm.getMonthId());
//		System.out.println(generateReportForm.getYearId());
//		
//		System.out.println("System Print Report");
		
		HashMap hashMap = new HashMap();
		hashMap.put("monthId", monthId);
		hashMap.put("yearId", yearId);
		hashMap.put("monthStr", monthStr[Integer.parseInt(monthId)-1]);
		
		Connection connection;
		ServletOutputStream servletOutputStream = response.getOutputStream();
		
		InputStream reportStream;
		
		if (ttlStr.equalsIgnoreCase("TTL-1")) {
			reportStream = getServlet().getServletConfig()
							.getServletContext().getResourceAsStream("/ireport/rptDistribution2Crosstab.jasper");
		}else {
			reportStream = getServlet().getServletConfig()
							.getServletContext().getResourceAsStream("/ireport/rptDistribution2CrosstabTTL2.jasper");
		}
		
		response.setContentType("application/pdf");
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

		connection = DriverManager.getConnection("jdbc:odbc:ttl51","sa","admin@123");
		JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, hashMap, connection);
		connection.close();
		servletOutputStream.flush();
		servletOutputStream.close();
		
		return mapping.getInputForward();
	}				
}
